# -*- coding: utf-8 -*-
import logging
import qrcode
import base64
from io import BytesIO
from odoo import api, fields, models
from PIL import Image,ImageFont,ImageDraw
from odoo import tools, _
from odoo.exceptions import ValidationError, AccessError
from odoo.modules.module import get_module_resource
import json
_logger = logging.getLogger(__name__)

class LineUp(models.Model):
    _name = 'line.up'
    _description = 'Service Contract Master Record'
    
    # 排队号
    name= fields.Char(string='Queue Number', required=True, copy=False, readonly=True, index=True, default=lambda self: _('PD'))
    # 手机设备号
    imei = fields.Char(string='IMEI',required=True)
    # 排队时间
    queuing_time = fields.Datetime(u'Queue Time', readonly=True, index=True, default=fields.Datetime.now)#,readonly=true,required=false
    # 等待人数
    waiting_number = fields.Integer(u'Waiting Number',readonly=True)
    # 属于POS
    config_id = fields.Many2one("pos.config", _("Belongs To POS"), readonly= True, default=lambda self: self.env.user.config_id)
    # 状态
    state = fields.Selection([
        ('draft', 'T'),
        ('audit', 'F'), 
    ], U'Status', readonly=True, index=True, copy=False, default='audit', track_visibility='onchange')
    active = fields.Boolean(u'Active', default=True, store=True)
    # 上桌状态
    active_sz = fields.Boolean(u'Serving Status', default=False)
    # APP用户
    pos_user_id = fields.Many2one("pos.user", _("APP User"), readonly=True)

    @api.multi
    def do_submission_done(self):
        for contract in self:
            contract.audit_status = 'draft'
            contract.state = 'audit'
        return True
    @api.multi
    def toggle_active(self):
        for contract in self:
            if contract.active:
                contract.state = 'audit'
                contract.active = False
                contract.active_sz = True
           
        return True
    @api.multi
    def toggle_active_sz(self):
        for contract in self:
            if contract.active:
                contract.active = False
            # else:
            #     contract.state = 'draft'
            #     contract.active = True
        return True

    @api.model
    def create(self, vals):
        if vals.get('name', _('New')) == _('New'):
            vals['name'] = self.env['ir.sequence'].next_by_code('lnie.up') or _('New')
        return super(LineUp, self).create(vals)
   
    @api.one
    @api.depends('order_line_ids')
    def _compute_total(self):
        """
        get and sum the order lines' price
        """
        self.total = sum(
            orderline.price for orderline in self.order_line_ids)
